<template>
  <div class="loginback">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <!-- xs屏幕视口小于768 左边就消失 也就是上面的盒子消失 -->
      <el-col :span="12" :xs="24">
        <el-form
          class="loginfrom"
          ref="ruleFormRef"
          :model="from"
          :rules="rules"
        >
          <h1>Hello</h1>
          <h3>欢迎</h3>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="from.username" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              show-password
              v-model="from.password"
            />
          </el-form-item>
          <el-button type="primary" @click="login" :loading="loading">
            登陆
          </el-button>
          <!--  :loading="loading" 按钮加载效果-->
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ElNotification } from "element-plus"
import { User, Lock } from "@element-plus/icons-vue" //这里引入的图表是一个组件
import { reactive, ref } from "vue"
import useCounterStore from "@/store/modules/user"
import { useRouter, useRoute } from "vue-router"
import { updateTime, gettime } from "@/utils/time"
//import type { FormInstance, FormRules } from 'element-plus'//引入表单校验规则
let user = useCounterStore()
const router = useRouter()
const $route = useRoute()
console.log($route.query.redirect, 123)

let from = reactive({
  username: "admin",
  password: "atguigu123",
})
// 控制按钮加载效果
const loading = ref(false)
// 邮箱电话格式校验 方法
let validatePass = (rule: any, value: any, callback: any) => {
  console.log(rule, "这里打印解决rule没有引用vscode爆红在登陆页")
  // console.log(value, "value");//输入框输入的东西
  // console.log(callback, "校验通不通过方法");//输入框输入的东西
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error("账户长度最少五位数"))
  }
}
let validatePass2 = (rule: any, value: any, callback: any) => {
  console.log(rule, "这里打印解决rule没有引用vscode爆红")
  // console.log(value, "value");//输入框输入的东西
  // console.log(callback, "校验通不通过方法");//输入框输入的东西
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error("密码长度最少6位数"))
  }
}

// 校验规则
const rules = {
  username: [
    { trigger: "change", validator: validatePass },
    // required: true, message: "账号名不能是空",
    // { min: 3, max: 5, message: "账号要3~5位数", trigger: "blur" },
  ],
  password: [
    { trigger: "change", validator: validatePass2 },
    // { required: true, message: "密码名不能是空", trigger: "blur" },
    // { min: 3, max: 6, message: "账号要3~6位数", trigger: "blur" },
  ],
}
let ruleFormRef = ref() //获取表单实例
// 登陆按钮一些流程操作
const login = async () => {
  // loading.value = true
  const Parameterjump: any = $route.query.redirect
  await ruleFormRef.value.validate() //先校验账号密码
  loading.value = true
  try {
    await user.LogService(from) //这里获取登陆成功或者失败的方法返回的结果
    if (Parameterjump) {
      router.push({ path: Parameterjump })
      ElNotification({
        title: ` HI${gettime()} ${updateTime()}`,
        message: "欢迎回来",
        type: "success",
      })
    } else {
      router.push("/")
      ElNotification({
        title: ` HI${gettime()} ${updateTime()}`,
        message: "登陆成功",
        type: "success",
      })
    }

    loading.value = false
  } catch (error) {
    ElNotification({ message: (<Error>error).message, type: "error" }) //类型断言的第二种写法
    //ElNotification({ message: (error as Error).message, type: 'error', })//类型断言的第二种写法
    // 这里的error这个变量被定义成 错误类型
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.loginback {
  width: 100%;
  // 视口高度
  height: 100vh;
  background-image: url("@/assets/images/error_images/back1.jpeg");

  background-repeat: no-repeat; //背景图不重复
  background-size: cover; //浏览器会根据视口自动设置图片大小

  .loginfrom {
    position: relative;
    width: 80%;
    top: 30vh; //视口的百分值30
    background-image: url("@/assets/images/login_form.png");
    background-repeat: no-repeat; //背景图不重复
    background-size: cover; //浏览器会根据视口自动设置图片大小
    padding: 40px;

    > h1 {
      color: #fff;
      font-size: 40;
    }

    > h3 {
      color: #fff;
      font-size: 20;
      margin: 20px 0 20px 0;
    }

    .el-button--primary {
      width: 100%;
    }
  }
}

::v-deep .el-form-item__error {
  color: #fff !important;
  left: 159px;
}
</style>
